package com.mohan.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Null;

import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mohan.bean.ArticleContentInfo;
import com.mohan.bean.ArticlePageInfo;
import com.mohan.bean.ContentImgInfo;
import com.mohan.common.LoginToken;
import com.mohan.pojo.Article;
import com.mohan.pojo.Comment;
import com.mohan.pojo.Content;
import com.mohan.pojo.Img;
import com.mohan.pojo.Reply;
import com.mohan.pojo.User;
import com.mohan.rep.ArticleListRep;
import com.mohan.rep.ArticleRep;
import com.mohan.rep.CommentRep;
import com.mohan.rep.ContentRep;
import com.mohan.rep.ImgRep;
import com.mohan.rep.ReplyRep;
import com.mohan.req.AddCommentReq;
import com.mohan.req.AddImgReq;
import com.mohan.req.ArticleListReq;
import com.mohan.req.ArticleReq;
import com.mohan.req.ChgArticleClassReq;
import com.mohan.req.ChgExamineReq;
import com.mohan.req.ChgIsOpenReq;
import com.mohan.req.ChgRecommendReq;
import com.mohan.req.ChgStatusReq;
import com.mohan.req.DeleteArticleReq;
import com.mohan.req.DeleteImgReq;
import com.mohan.req.ManagerAddArticleReq;
import com.mohan.req.UpdateArticleReq;
import com.mohan.req.UpdateImgReq;
import com.mohan.service.ArticleService;
import com.mohan.service.UserService;
import com.mohan.tool.Base64CC;
import com.mohan.tool.UserMap;
import com.mohan.util.ArticleUp;
import com.mohan.util.ResponseUtil;

import javafx.collections.ListChangeListener.Change;

@Controller
@RequestMapping(value = "/V1/article")
public class ArticleController {

	@Resource
	private ArticleService articleService;

	@Resource
	private UserService userService;

	/**
	 * 创建文章
	 * 
	 * @param request
	 * @param token
	 * @param userid
	 * @return
	 */

	@RequestMapping(value = "/add")
	@ResponseBody		
	public Object addArticle(@RequestBody ManagerAddArticleReq articleReq, HttpServletRequest request) {
		String repStr = null;
		int errCode = 0;
		int articleId = -1;
		/**
		 * 判断上传的参数
		 */
		if (articleReq.getUserId() == null || articleReq.getToken() == null || articleReq.getArticle() == null) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				articleReq.setUserId(Base64CC.decode(articleReq.getUserId()));
				articleReq.setToken(Base64CC.decode(articleReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(articleReq.getUserId(), articleReq.getToken());
				if (loginConfirm == 0) {
					// 登录验证成功
					/**
					 * base64解码文章内容
					 */
					if (articleReq.getArticle().getTitle() == null || "".equals(articleReq.getArticle().getTitle())) {
						repStr = "请添加文章标题!";
						errCode = 2025;
					} else {
						System.out.println("title = " + Base64CC.decode(articleReq.getArticle().getTitle()));
						articleReq.getArticle().setTitle(Base64CC.decode(articleReq.getArticle().getTitle()));
						if (articleReq.getArticle().getUserId() == null
								|| "".equals(articleReq.getArticle().getUserId())) {
							repStr = "请添加用户id!";
							errCode = 2026;
						} else {
							System.out.println("userId = " + Base64CC.decode(articleReq.getArticle().getUserId()));
							articleReq.getArticle().setUserId(Base64CC.decode(articleReq.getArticle().getUserId()));
							if (articleReq.getArticle().getType()<1 || articleReq.getArticle().getType()>10) {
								repStr = "请添加文章分类!";
								errCode = 2027;
							} else {
								System.out.println("type = " + articleReq.getArticle().getType());
								if (articleReq.getArticle().getContents().size() == 0) {
									repStr = "请添加文章内容!";
									errCode = 2028;
								} else {
									boolean roleConfirm = true;
									if (articleReq.getArticle().getArticleClass()<1 || articleReq.getArticle().getArticleClass()>2) {
										// 默认为用户文章
										articleReq.getArticle().setArticleClass(2);
									} else if (articleReq.getArticle().getArticleClass() == 1) {
										/**
										 * 权限验证 只有管理员才能添加系统文章
										 */
										System.out.println("userID = " + articleReq.getUserId());
										if (UserMap.UserId2TokenMap.get(articleReq.getUserId()).getRole() > 3) {
											roleConfirm = false;
											repStr = "用户权限不够!";
											errCode = 2015;
										}
									}
									if (roleConfirm) {
										if (articleReq.getArticle().getCountry() == null
												|| "".equals(articleReq.getArticle().getCountry())) {
											articleReq.getArticle().setCountry("中国");
										} else {
											articleReq.getArticle()
													.setCountry(Base64CC.decode(articleReq.getArticle().getCountry()));
										}
										if (articleReq.getArticle().getAuthor() == null
												|| "".equals(articleReq.getArticle().getAuthor())) {
											articleReq.getArticle().setAuthor("佚名");
										} else {
											articleReq.getArticle()
													.setAuthor(Base64CC.decode(articleReq.getArticle().getAuthor()));
										}
										if (articleReq.getArticle().getDynasty() == null
												|| "".equals(articleReq.getArticle().getDynasty())) {
											articleReq.getArticle().setDynasty("未知");
										} else {
											articleReq.getArticle()
													.setDynasty(Base64CC.decode(articleReq.getArticle().getDynasty()));
										}
										if (articleReq.getArticle().getCreateTime() == null
												|| "".equals(articleReq.getArticle().getCreateTime())) {
											articleReq.getArticle().setCreateTime("0000-00-00 00:00:00");
										} else {
											articleReq.getArticle().setCreateTime(
													Base64CC.decode(articleReq.getArticle().getCreateTime()));
										}
										if (articleReq.getArticle().getBook() == null
												|| "".equals(articleReq.getArticle().getBook())) {
											articleReq.getArticle().setBook("未知");
										} else {
											articleReq.getArticle()
													.setBook(Base64CC.decode(articleReq.getArticle().getBook()));
										}
										if (articleReq.getArticle().getStyle() == null
												|| "".equals(articleReq.getArticle().getStyle())) {
											articleReq.getArticle().setStyle("未知");
										} else {
											articleReq.getArticle()
													.setStyle(Base64CC.decode(articleReq.getArticle().getStyle()));
										}
										if (articleReq.getArticle().getTags() == null
												|| "".equals(articleReq.getArticle().getTags())) {
											articleReq.getArticle().setTags("未知");
										} else {
											articleReq.getArticle()
													.setTags(Base64CC.decode(articleReq.getArticle().getTags()));
										}
										if (articleReq.getArticle().getArticleProfile() == null
												|| "".equals(articleReq.getArticle().getArticleProfile())) {
											articleReq.getArticle().setArticleProfile("未知");
										} else {
											articleReq.getArticle().setArticleProfile(
													Base64CC.decode(articleReq.getArticle().getArticleProfile()));
										}
										if (articleReq.getArticle().getAnalysis() == null
												|| "".equals(articleReq.getArticle().getAnalysis())) {
											articleReq.getArticle().setAnalysis("未知");
										} else {
											articleReq.getArticle().setAnalysis(
													Base64CC.decode(articleReq.getArticle().getAnalysis()));
										}
										if (articleReq.getArticle().getIsOpen()<1 || articleReq.getArticle().getIsOpen()>3) {
											articleReq.getArticle().setIsOpen(3);
										}
										if (articleReq.getArticle().getArticleStatus()<1 || articleReq.getArticle().getArticleStatus()>4) {
											// 默认发布
											articleReq.getArticle().setArticleStatus(2);
										}
										/**
										 * base64解码content内容
										 */
										boolean isOK = true;

										List<ArticleContentInfo> contentInfos = articleReq.getArticle().getContents();
										System.out.println("contentSize = " + contentInfos.size());
										for (ArticleContentInfo articleContentInfo : contentInfos) {
											if (articleContentInfo.getContent() == null
													|| "".equals(articleContentInfo.getContent())) {
												System.out.println("content error ...");
												isOK = false;
												repStr = "文段内容不能为空";
												break;
											} else {
												if (articleContentInfo.getSerialNum() < 0) {
													isOK = false;
													repStr = "文段编号不能为空";
													errCode = 2029;
													break;
												}else {

													articleContentInfo
															.setContent(Base64CC.decode(articleContentInfo.getContent()));
													if (articleContentInfo.getFontColor() == null
															|| "".equals(articleContentInfo.getFontColor())) {
														articleContentInfo.setFontColor("#000000");
													} else {
														articleContentInfo.setFontColor(
																Base64CC.decode(articleContentInfo.getFontColor()));
													}
													if (articleContentInfo.getFontSize() == 0) {
														articleContentInfo.setFontSize(14);
													} else {
														articleContentInfo.setFontSize(articleContentInfo.getFontSize());
													}
													if (articleContentInfo.getAudioUrl() != null) {
														articleContentInfo.setAudioUrl(
																Base64CC.decode(articleContentInfo.getAudioUrl()));
													}
													if (articleContentInfo.getVideoUrl() != null) {
														articleContentInfo.setVideoUrl(
																Base64CC.decode(articleContentInfo.getVideoUrl()));
													}

													if (articleContentInfo.getImgs() != null) {
														if (articleContentInfo.getImgs().size() == 0) {
															System.out.println("imgSize = 0");
														} else {
															List<ContentImgInfo> imgInfos = articleContentInfo.getImgs();
															System.out.println("imgSize = " + imgInfos.size());
															for (ContentImgInfo contentImgInfo : imgInfos) {
																if (contentImgInfo.getImgPosition()<1 || contentImgInfo.getImgPosition()>3) {
																	// 默认段尾
																	contentImgInfo.setImgPosition(3);
																}
																if (!(contentImgInfo.getImgUrl() == null
																		|| "".equals(contentImgInfo.getImgUrl()))) {
																	contentImgInfo.setImgUrl(
																			Base64CC.decode(contentImgInfo.getImgUrl()));
																}
																if (!(contentImgInfo.getImgSize() == null
																		|| "".equals(contentImgInfo.getImgSize()))) {
																	contentImgInfo.setImgSize(
																			Base64CC.decode(contentImgInfo.getImgSize()));
																}
															}
														}
													}
												}
											}
										}

										System.out.println("isOK = " + isOK);
										if (isOK) {
											/**
											 * 插入数据库
											 */
											System.out.println("start insert db ......");
											// 1.插入article
											Article article = new Article();
											article.setTitle(articleReq.getArticle().getTitle());
											article.setCountry(articleReq.getArticle().getCountry());
											article.setAuthor(articleReq.getArticle().getAuthor());
											article.setDynasty(articleReq.getArticle().getDynasty());
											article.setCreateTime(articleReq.getArticle().getCreateTime());
											article.setBook(articleReq.getArticle().getBook());
											article.setStyle(articleReq.getArticle().getStyle());
											article.setTags(articleReq.getArticle().getTags());
											article.setArticleProfile(articleReq.getArticle().getArticleProfile());
											article.setAnalysis(articleReq.getArticle().getAnalysis());
											article.setType(articleReq.getArticle().getType());
											article.setIsOpen(articleReq.getArticle().getIsOpen());
											article.setArticleStatus(articleReq.getArticle().getArticleStatus());
											article.setArticleClass(articleReq.getArticle().getArticleClass());
											article.setUserId(articleReq.getArticle().getUserId());
											article.setLoves(0);
											article.setExamine(3);
											article.setRecommend(1);
											article.setJurisdiction("3213");
											articleService.insertOneArticle(article);

											// 获取articleId
											articleId = articleService.getLastArticleId();
											System.out.println("articleId = " + articleId);
											/**
											 * 插入文段
											 */
//											List<ArticleContentInfo> contents = articleReq.getArticle().getContents();
											List<Integer> contentIds = new ArrayList<Integer>();

											for (ArticleContentInfo contentInfo : contentInfos) {
												Content content = new Content();
												content.setArticleId(articleId);
												content.setAudioUrl(contentInfo.getAudioUrl());
												content.setAuthor(article.getAuthor());
												content.setContent(contentInfo.getContent());
												content.setFontColor(contentInfo.getFontColor());
												content.setFontSize(contentInfo.getFontSize());
												content.setSerialNum(contentInfo.getSerialNum());
												content.setUserId(article.getUserId());
												content.setVideoUrl(contentInfo.getVideoUrl());
												articleService.insertContent(content);
												int contentId = articleService.getLastContentId();
												System.out.println("contentid = " + contentId);
												contentIds.add(contentId);

												/**
												 * 把img信息插入数据库
												 */
												List<ContentImgInfo> imgs = contentInfo.getImgs();
												List<Integer> imgIds = new ArrayList<Integer>();
												for (ContentImgInfo imgInfo : imgs) {
													Img img = new Img();
													img.setArticleId(articleId);
													img.setContentId(contentId);
													img.setImgPosition(imgInfo.getImgPosition());
													img.setImgSize(imgInfo.getImgSize());
													img.setImgUrl(imgInfo.getImgUrl());
													img.setUserId(content.getUserId());
													articleService.insertOneImg(img);
													int imgId = articleService.getLastImgId();
													imgIds.add(imgId);
												}
												/**
												 * 把imgIds插入content表
												 */
												if (imgIds.size() > 0) {
													String imgIdStr = "[";
													for (Integer integer : imgIds) {
														imgIdStr = imgIdStr + String.valueOf(integer) + ",";
													}
													imgIdStr = imgIdStr.substring(0, imgIdStr.length() - 1);
													imgIdStr = imgIdStr + "]";
													Content content2 = new Content();
													content2.setContentId(contentId);
													content2.setImgIds(imgIdStr);
													articleService.updateImgIds(content2);
												}
											}
											/**
											 * 把段落id数组插入文章列表
											 */
											if (contentIds.size() > 0) {
												String contentIdStr = "[";
												for (Integer integer : contentIds) {
													contentIdStr = contentIdStr + String.valueOf(integer) + ",";
												}
												contentIdStr = contentIdStr.substring(0, contentIdStr.length() - 1);
												contentIdStr = contentIdStr + "]";
												Article article2 = new Article();
												article2.setArticleId(articleId);
												article2.setContentIds(contentIdStr);
												articleService.updateContentIds(article2);
											}
										} else {
											repStr = "文本内容问题!";
											errCode = 2030;
										}
									}
								}
							}
						}
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}

		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			response.defaultSucess(articleId);
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}

	/**
	 * 添加一个插图
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/addImg")
	@ResponseBody
	public Object addImg(HttpServletRequest request, @RequestBody AddImgReq addReq) {
		String repStr = null;
		int errCode = 0;
		/**
		 * 判断上传的参数
		 */
		if (addReq.getUserId() == null || addReq.getToken() == null || addReq.getArticleId() < 1
				|| addReq.getContentId() < 1 || addReq.getImg() == null || addReq.getImg().getImgUrl() == null) {
			repStr = "参数上传错误!";
			errCode = 2001; 
		} else {
			try {
				/**
				 * base64解码
				 */
				addReq.setUserId(Base64CC.decode(addReq.getUserId()));
				addReq.setToken(Base64CC.decode(addReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(addReq.getUserId(), addReq.getToken());
				if (loginConfirm == 0) {
					/**
					 * 权限验证 该用户是否是该文章的拥有者或超级用户
					 */
					// 根据文章id获取所属用户id的role
					// int userRole =
					// articleService.getRoleByArticleId(imgReq.getArticleId());
					// System.out.println("user role is " + userRole);
					// 根据文章id获取文章所属用户id
					Article article = articleService.getArticle(addReq.getArticleId());
					if (article != null
							&& article.getContentIds().indexOf(String.valueOf(addReq.getContentId())) != -1) {
						// 根据用户id获取用户信息
						User articleUser = userService.getUserByUserId(article.getUserId());
						if (articleUser == null) {
							repStr = "文章所属用户为空!";
							errCode = 2035; 
						} else {
							User articleUserInfo = articleUser;
							if (addReq.getUserId().equals(article.getUserId()) || UserMap.UserId2TokenMap
									.get(addReq.getUserId()).getRole() < articleUserInfo.getRole()) {
								// 插入插图
								Img img = new Img();
								img.setArticleId(addReq.getArticleId());
								img.setContentId(addReq.getContentId());
								if (addReq.getImg().getImgPosition() == 0) {
									img.setImgPosition(3);
								} else {
									img.setImgPosition(addReq.getImg().getImgPosition());
								}
								if (addReq.getImg().getImgSize() != null) {
									img.setImgSize(Base64CC.decode(addReq.getImg().getImgSize()));
								}
								img.setImgUrl(Base64CC.decode(addReq.getImg().getImgUrl()));
								img.setUserId(articleUserInfo.getUserId());

								articleService.insertOneImg(img);

								if (updateImgIds(addReq.getContentId()) != 0) {
									repStr = "更新imgId错误!";
									errCode = 2036; 
								}
							} else {
								repStr = "用户操作权限不够!";
								errCode = 2015;
							}
						}
					} else {
						repStr = "要修改的文章或文段不存在!";
						errCode = 2031;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}

		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			try {
				response.defaultSucess(Base64CC.encode("插图添加成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}

	/**
	 * 删除插图
	 * 
	 * @param request
	 * @param deleteReq
	 * @return
	 */
	@RequestMapping("/deleteImg")
	@ResponseBody
	public Object deleteImg(HttpServletRequest request, @RequestBody DeleteImgReq deleteReq) {
		String repStr = null;
		int errCode = 0;
		if (deleteReq.getUserId() == null || deleteReq.getToken() == null || deleteReq.getImgId() < 1) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				deleteReq.setUserId(Base64CC.decode(deleteReq.getUserId()));
				deleteReq.setToken(Base64CC.decode(deleteReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(deleteReq.getUserId(), deleteReq.getToken());
				if (loginConfirm == 0) {
					/**
					 * 权限验证
					 */
					// 根据插图id获取插图信息
					Img img = articleService.getImgByImgId(deleteReq.getImgId());
					if (img == null) {
						repStr = "要删除的插图不存在!";
						errCode = 2037;
					} else {
						// 根据userId获取用户信息
						User user = userService.getUserByUserId(img.getUserId());
						if (user != null) {
							if (deleteReq.getUserId().equals(img.getUserId()) || user
									.getRole() > UserMap.UserId2TokenMap.get(deleteReq.getUserId()).getRole()) {
								articleService.deleteImgByImgId(img.getImgId());
								if (updateImgIds(img.getContentId()) != 0) {
									repStr = "更新imgId错误!";
									errCode = 2036;
								}
							} else {
								repStr = "用户权限不够!";
								errCode = 2015;
							}
						} else {
							repStr = "无法获取插图所属用户信息!";
							errCode = 2038;
						}
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}
		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			try {
				response.defaultSucess(Base64CC.encode("插图删除成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}

	/**
	 * 更新插图
	 * 
	 * @param request
	 * @param updateReq
	 * @return
	 */
	@RequestMapping("/updateImg")
	@ResponseBody
	public Object updateImg(HttpServletRequest request, @RequestBody UpdateImgReq updateReq) {
		String repStr = null;
		int errCode = 0;
		if (updateReq.getUserId() == null || updateReq.getToken() == null || updateReq.getImgId() < 1
				|| updateReq.getImg() == null) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				updateReq.setUserId(Base64CC.decode(updateReq.getUserId()));
				updateReq.setToken(Base64CC.decode(updateReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(updateReq.getUserId(), updateReq.getToken());
				if (loginConfirm == 0) {
					/**
					 * 权限验证
					 */
					// 根据插图id获取插图信息
					Img img = articleService.getImgByImgId(updateReq.getImgId());
					if (img != null) {
						// 根据userId获取用户信息
						User user = userService.getUserByUserId(img.getUserId());
						if (user != null) {
							if (updateReq.getUserId().equals(img.getUserId()) || user
									.getRole() > UserMap.UserId2TokenMap.get(updateReq.getUserId()).getRole()) {
								if (updateReq.getImg().getImgUrl() == null
										&& updateReq.getImg().getImgPosition() == 0) {
									repStr = "没有可更新参数!";
									errCode = 2039;
								} else {
									if (updateReq.getImg().getImgUrl() != null) {
										img.setImgUrl(Base64CC.decode(updateReq.getImg().getImgUrl()));
									}
									if (updateReq.getImg().getImgSize() != null) {
										img.setImgSize(Base64CC.decode(updateReq.getImg().getImgSize()));
									}
									if (updateReq.getImg().getImgPosition() != 0) {
										img.setImgPosition(updateReq.getImg().getImgPosition());
									}
									articleService.updateImgByImgId(img);
								}
							} else {
								repStr = "用户权限不够!";
								errCode = 2015;
							}
						} else {
							repStr = "不存在要更新的插图!";
							errCode = 2037;
						}
					} else if (loginConfirm == -1) {
						repStr = "请先登录!";
						errCode = 2016;
					} else if (loginConfirm == -2) {
						repStr = "账号异常!";
						errCode = 2017;
					} else {
						repStr = "验证码过期,请重新登录!";
						errCode = 2018;
					}
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}
		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			try {
				response.defaultSucess(Base64CC.encode("插图更新成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}
	
	/**
	 * 根据文章id删除文章
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object deleteArticle(HttpServletRequest request, @RequestBody DeleteArticleReq deleteReq) {
		String repStr = null;
		int errCode = 0;
		if (deleteReq.getUserId() == null || deleteReq.getToken() == null || deleteReq.getArticleId()<1) {
			repStr = "参数上传错误!";
			 errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				deleteReq.setUserId(Base64CC.decode(deleteReq.getUserId()));
				deleteReq.setToken(Base64CC.decode(deleteReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(deleteReq.getUserId(), deleteReq.getToken());
				if (loginConfirm == 0) {
					/**
					 * 权限验证
					 * 本人和超级用户才能删除文章
					 */
					//根据文章id获取文章信息
					Article article = articleService.getArticle(deleteReq.getArticleId());
					if (article !=null) {
						if (article.getUserId().equals(deleteReq.getUserId()) || UserMap.UserId2TokenMap.get(deleteReq.getUserId()).getRole()==1) {
							//更改文章状态为删除
							articleService.deleteArticle(deleteReq.getArticleId());
						}
					}else {
						repStr = "要删除的文章不存在!";
						 errCode = 2031;
					}
					
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}

		}
		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			try {
				response.defaultSucess(Base64CC.encode("文章删除成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}

	/**
	 * 修改文章
	 * 
	 * @param request
	 * @param token
	 * @param userid
	 * @param article
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object updateArticle(HttpServletRequest request, @RequestBody UpdateArticleReq updateReq) {
		String repStr = null;
		int errCode = 0;
		if (updateReq.getUserId()==null || updateReq.getToken()==null || updateReq.getArticleId()<1 || updateReq.getArticle()==null) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				updateReq.setUserId(Base64CC.decode(updateReq.getUserId()));
				updateReq.setToken(Base64CC.decode(updateReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(updateReq.getUserId(), updateReq.getToken());
				if (loginConfirm == 0) {
					/**
					 * 权限验证
					 * 只有超级用户和本人能够修改
					 */
					//根据articleId获取文章信息
					ArticleUp articleNew = updateReq.getArticle();
					Article articleOld = articleService.getArticle(updateReq.getArticleId());
					if (articleOld == null) {
						repStr = "没有要修改的文章!";
						errCode = 2031;
					} else {
						if (updateReq.getUserId().equals(articleOld.getUserId()) || UserMap.UserId2TokenMap.get(updateReq.getUserId()).getRole()==1) {
							if (articleOld.getArticleStatus()==1 || articleOld.getArticleStatus()==2) {
								if (articleOld.getContentIds() != null) {
									/**
									 * 1.更新article(不包含contents)
									 */
									if (updateReq.getArticle().getTitle() != null && (!"".equals(updateReq.getArticle().getTitle()))) {
										articleOld.setTitle(Base64CC.decode(updateReq.getArticle().getTitle()));
									}
									if (updateReq.getArticle().getCountry() != null && (!"".equals(updateReq.getArticle().getCountry()))) {
										articleOld.setCountry(Base64CC.decode(updateReq.getArticle().getCountry()));
									}
									if (updateReq.getArticle().getAuthor() != null && (!"".equals(updateReq.getArticle().getAuthor()))) {
										articleOld.setAuthor(Base64CC.decode(updateReq.getArticle().getAuthor()));
									}
									if (updateReq.getArticle().getBook() != null && (!"".equals(updateReq.getArticle().getBook()))) {
										articleOld.setBook(Base64CC.decode(updateReq.getArticle().getBook()));
									}
									if (updateReq.getArticle().getStyle() != null && (!"".equals(updateReq.getArticle().getStyle()))) {
										articleOld.setStyle(Base64CC.decode(updateReq.getArticle().getStyle()));
									}
									if (updateReq.getArticle().getTags() != null && (!"".equals(updateReq.getArticle().getTags()))) {
										articleOld.setTags(Base64CC.decode(updateReq.getArticle().getTags()));
									}
									if (updateReq.getArticle().getArticleProfile() != null && (!"".equals(updateReq.getArticle().getArticleProfile()))) {
										articleOld.setArticleProfile(Base64CC.decode(updateReq.getArticle().getArticleProfile()));
									}
									if (updateReq.getArticle().getAnalysis() != null && (!"".equals(updateReq.getArticle().getAnalysis()))) {
										articleOld.setAnalysis(Base64CC.decode(updateReq.getArticle().getAnalysis()));
									}
									if (updateReq.getArticle().getType() > 0) {
										articleOld.setType(updateReq.getArticle().getType());
									}
									if (updateReq.getArticle().getIsOpen() > 0) {
										articleOld.setIsOpen(updateReq.getArticle().getIsOpen());
									}
									if (updateReq.getArticle().getArticleStatus() > 0) {
										articleOld.setArticleStatus(updateReq.getArticle().getArticleStatus());
									}
									if (updateReq.getArticle().getArticleClass() > 0) {
										articleOld.setArticleClass(updateReq.getArticle().getArticleClass());
									}
									articleService.updateArticleByArticleId(articleOld);
									
									/**
									 * 文段的插入
									 */
									JSONArray oldContentIdArr = new JSONArray(articleOld.getContentIds());
									System.out.println("oldContentSize = " + oldContentIdArr.length() + " ; newContentSize = " + articleNew.getContents().size());
									if (oldContentIdArr.length() >= articleNew.getContents().size()) {
										/**
										 * 文段减少或没有变
										 * 覆盖旧的,删除多余的
										 */
										for (int i = 0; i < articleNew.getContents().size(); i++) {
											
											Content oldContent = articleService.getContentByContentId(oldContentIdArr.getInt(i));
											ArticleContentInfo newContent = articleNew.getContents().get(i);
											
											System.out.println("newContent Serlnum = " + newContent.getSerialNum());
											System.out.println("oldContent Serlnum = " + oldContent.getSerialNum() + "; id = " + oldContent.getContentId());
											
											if (newContent.getAudioUrl() != null && (!"".equals(newContent.getAudioUrl()))) {
												oldContent.setAudioUrl(Base64CC.decode(newContent.getAudioUrl()));
											}
											if (newContent.getContent() != null && (!"".equals(newContent.getContent()))) {
												oldContent.setContent(Base64CC.decode(newContent.getContent()));
											}
											if (newContent.getFontColor() != null && (!"".equals(newContent.getFontColor()))) {
												oldContent.setFontColor(Base64CC.decode(newContent.getFontColor()));
											}
											if (newContent.getFontSize() > 0) {
												oldContent.setFontSize(newContent.getFontSize());
											}
											if (newContent.getSerialNum() > 0) {
												oldContent.setSerialNum(newContent.getSerialNum());
											}
											if (newContent.getVideoUrl() != null && (!"".equals(newContent.getVideoUrl()))) {
												oldContent.setVideoUrl(Base64CC.decode(newContent.getVideoUrl()));
											}
											oldContent.setAuthor(articleOld.getAuthor());
											articleService.updateContent(oldContent);
											updateContentImgs(newContent,oldContent.getContentId());
										}
										for (int i = articleNew.getContents().size(); i < oldContentIdArr.length(); i++) {
											Content oldContent = articleService.getContentByContentId(oldContentIdArr.getInt(i));
											if (oldContent != null) {
												//删除段落的插图
												if (oldContent.getImgIds() != null) {
													JSONArray oldimgIdArr = new JSONArray(oldContent.getImgIds());
													for (int j = 0; j < oldimgIdArr.length(); j++) {
														articleService.deleteImgByImgId(oldimgIdArr.getInt(j));
													}
												}
												//删除段落
												articleService.deleteContent(oldContentIdArr.getInt(i));
											}
										}
									}else{
										/**
										 * 文段增加
										 * 覆盖旧的,添加新的
										 */
										for (int i = 0; i < oldContentIdArr.length(); i++) {
											Content oldContent = articleService.getContentByContentId(oldContentIdArr.getInt(i));
											ArticleContentInfo newContent = articleNew.getContents().get(i);
											if (newContent.getAudioUrl() != null && (!"".equals(newContent.getAudioUrl()))) {
												oldContent.setAudioUrl(Base64CC.decode(newContent.getAudioUrl()));
											}
											if (newContent.getContent() != null && (!"".equals(newContent.getContent()))) {
												oldContent.setContent(Base64CC.decode(newContent.getContent()));
											}
											if (newContent.getFontColor() != null && (!"".equals(newContent.getFontColor()))) {
												oldContent.setFontColor(Base64CC.decode(newContent.getFontColor()));
											}
											if (newContent.getFontSize() > 0) {
												oldContent.setFontSize(newContent.getFontSize());
											}
											if (newContent.getSerialNum() > 0) {
												oldContent.setSerialNum(newContent.getSerialNum());
											}
											if (newContent.getVideoUrl() != null && (!"".equals(newContent.getVideoUrl()))) {
												oldContent.setVideoUrl(Base64CC.decode(newContent.getVideoUrl()));
											}
											oldContent.setAuthor(articleOld.getAuthor());
											articleService.updateContent(oldContent);
											updateContentImgs(newContent,oldContent.getContentId());
										}
										//插入新的
										for (int i = oldContentIdArr.length(); i < articleNew.getContents().size(); i++) {
											ArticleContentInfo contentInfo = articleNew.getContents().get(i);
											Content content = new Content();
											
											if (contentInfo.getFontSize() < 1) {
												content.setFontSize(14);
											}else {
												content.setFontSize(contentInfo.getFontSize());
											}
											if (contentInfo.getFontColor() == null) {
												content.setFontColor("#000000");
											} else {
												content.setFontColor(Base64CC.decode(contentInfo.getFontColor()));
											}
											if (contentInfo.getAudioUrl() != null && (!"".equals(contentInfo.getAudioUrl()))) {
												content.setAudioUrl(Base64CC.decode(contentInfo.getAudioUrl()));
											}
											if (contentInfo.getVideoUrl() != null && (!"".equals(contentInfo.getVideoUrl()))) {
												content.setVideoUrl(Base64CC.decode(contentInfo.getVideoUrl()));
											}
											if (contentInfo.getContent() != null && (!"".equals(contentInfo.getContent()))) {
												content.setContent(Base64CC.decode(contentInfo.getContent()));
											}
											content.setArticleId(articleOld.getArticleId());
											content.setAuthor(articleOld.getAuthor());
											content.setSerialNum(contentInfo.getSerialNum());
											content.setUserId(articleOld.getUserId());
											articleService.insertContent(content);
											
											int contentId = articleService.getLastContentId();
											System.out.println("contentid = " + contentId);
											

											/**
											 * 把img信息插入数据库
											 */
											List<ContentImgInfo> imgs = contentInfo.getImgs();
											for (ContentImgInfo imgInfo : imgs) {
												Img img = new Img();
												img.setArticleId(articleOld.getArticleId());
												img.setContentId(contentId);
												img.setImgPosition(imgInfo.getImgPosition());
												img.setImgSize(imgInfo.getImgSize());
												img.setImgUrl(imgInfo.getImgUrl());
												img.setUserId(content.getUserId());
												articleService.insertOneImg(img);
											}
											/**
											 * 把imgIds插入content表
											 */
											updateImgIds(contentId);
										}
									}
									/**
									 * 把contentIds插入article表
									 */
									updateContentIds(articleOld.getArticleId());
								} else {
									repStr = "必须有文段内容!";
									errCode = 2032;
								}
							}else if (articleOld.getArticleStatus()==3) {
								//文章已经下架
								repStr = "要修改的文章已经下架!";
								errCode = 2033;
							}else if (articleOld.getArticleStatus()==4) {
								//文章已经删除
								repStr = "要修改的文章已经被删除!";
								errCode = 2034;
							}else {
								//未知错误
								repStr = "系统错误";
								errCode = 2005;
							}
						} else {
							repStr = "用户操作权限不够!";
							errCode = 2015;
						}
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}
		
		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			try {
				response.defaultSucess(Base64CC.encode("文章修改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}

	/**
	 * 分页获取文章列表
	 * @param request
	 * @param listReq
	 * @return
	 */
	@RequestMapping(value = "/list") // 设置访问方法method=RequestMethod.POST
	@ResponseBody
	public Object getArticleList(HttpServletRequest request, @RequestBody ArticleListReq listReq) {
		String repStr = null;
		int errCode = 0;
		ArticleListRep listRep = new ArticleListRep();
		if (listReq.getUserId()==null || listReq.getToken()==null || listReq.getArticleList()==null) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				listReq.setUserId(Base64CC.decode(listReq.getUserId()));
				listReq.setToken(Base64CC.decode(listReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(listReq.getUserId(), listReq.getToken());
				if (loginConfirm == 0) {
					/**
					 * 请求参数验证
					 */
					if (listReq.getArticleList().getUserId() != null) {
						//解码userId
						listReq.getArticleList().setUserId(Base64CC.decode(listReq.getArticleList().getUserId()));
								if (listReq.getArticleList().getTotalRecord() < 1) {
									/**
									 * 没有记录的总条数,从数据库获取
									 */
									Integer articleSize = articleService.getCountByUserId(listReq.getArticleList().getUserId());
									if (articleSize == null) {
										//该用户下没有文章
										listReq.getArticleList().setTotalRecord(0);
									}else{
										listReq.getArticleList().setTotalRecord(articleSize.intValue());
										if (listReq.getArticleList().getEachPageSize() < 1) {
											listReq.getArticleList().setEachPageSize(20);
										}
										int pageSize = (int)Math.ceil(listReq.getArticleList().getTotalRecord()/(listReq.getArticleList().getEachPageSize()*1.0));
										listReq.getArticleList().setTotalPages(pageSize);
										if (listReq.getArticleList().getQueryPage() < 1){
											listReq.getArticleList().setQueryPage(1);//获取第一页
										}else if (listReq.getArticleList().getQueryPage() > pageSize) {
											listReq.getArticleList().setQueryPage(pageSize);//获取第一页
										}
										listReq.getArticleList().setStart((listReq.getArticleList().getQueryPage()-1)*listReq.getArticleList().getEachPageSize());
										List<Article> aList = new ArrayList<Article>();
										
										/**
										 * 权限验证
										 */
										if (UserMap.UserId2TokenMap.get(listReq.getUserId()).getRole() < 3) {
											/**
											 * 此处是管理员,可以获取删除的文章
											 */
											aList = articleService.getArticleListByPageM(listReq.getArticleList());
										}else{
											if (listReq.getUserId().equals(listReq.getArticleList().getUserId())) {
												/**
												 * 此处是自身
												 */
												aList = articleService.getArticleListByPageS(listReq.getArticleList());
											}else {
												/**
												 * 其他用户
												 */
												aList = articleService.getArticleListByPageO(listReq.getArticleList());
											}
										}
										List<ArticlePageInfo> articelPages = new ArrayList<ArticlePageInfo>();
										if (!aList.isEmpty()) {
											for (Article article : aList) {
												ArticlePageInfo aInfo = new ArticlePageInfo();
												aInfo.setArticleId(article.getArticleId());
												aInfo.setAuthor(Base64CC.encode(article.getAuthor()));
												aInfo.setCreateTime(Base64CC.encode(article.getCreateTime()));
												aInfo.setDynasty(Base64CC.encode(article.getDynasty()));
												aInfo.setTitle(Base64CC.encode(article.getTitle()));
												articelPages.add(aInfo);
											}
											listRep.setArticelPage(articelPages);
											listRep.setEachPageSize(listReq.getArticleList().getEachPageSize());
											listRep.setQueryPage(listReq.getArticleList().getQueryPage());
											listRep.setTotalPages(listReq.getArticleList().getTotalPages());
											listRep.setTotalRecord(listReq.getArticleList().getTotalRecord());
											listRep.setUserId(Base64CC.encode(listReq.getArticleList().getUserId()));
										}
									}
								}else {
									/**
									 * 带了记录的总条数和相关参数
									 */
									if (listReq.getArticleList().getQueryPage()>0 && listReq.getArticleList().getQueryPage()<=listReq.getArticleList().getTotalPages() && listReq.getArticleList().getEachPageSize()>0) {
										listReq.getArticleList().setStart((listReq.getArticleList().getQueryPage()-1)*listReq.getArticleList().getEachPageSize());
										List<Article> aList = new ArrayList<Article>();
										
										/**
										 * 权限验证
										 */
										if (UserMap.UserId2TokenMap.get(listReq.getUserId()).getRole() < 3) {
											/**
											 * 此处是管理员,可以获取删除的文章
											 */
											aList = articleService.getArticleListByPageM(listReq.getArticleList());
										}else{
											if (listReq.getUserId().equals(listReq.getArticleList().getUserId())) {
												/**
												 * 此处是自身
												 */
												aList = articleService.getArticleListByPageS(listReq.getArticleList());
											}else {
												/**
												 * 其他用户
												 */
												aList = articleService.getArticleListByPageO(listReq.getArticleList());
											}
										}
										List<ArticlePageInfo> articelPages = new ArrayList<ArticlePageInfo>();
										if (!aList.isEmpty()) {
											for (Article article : aList) {
												ArticlePageInfo aInfo = new ArticlePageInfo();
												aInfo.setArticleId(article.getArticleId());
												aInfo.setAuthor(Base64CC.encode(article.getAuthor()));
												aInfo.setCreateTime(Base64CC.encode(article.getCreateTime()));
												aInfo.setDynasty(Base64CC.encode(article.getDynasty()));
												aInfo.setTitle(Base64CC.encode(article.getTitle()));
												articelPages.add(aInfo);
											}
											listRep.setArticelPage(articelPages);
											listRep.setEachPageSize(listReq.getArticleList().getEachPageSize());
											listRep.setQueryPage(listReq.getArticleList().getQueryPage());
											listRep.setTotalPages(listReq.getArticleList().getTotalPages());
											listRep.setTotalRecord(listReq.getArticleList().getTotalRecord());
											listRep.setUserId(Base64CC.encode(listReq.getArticleList().getUserId()));
										}
									}else {
										repStr = "参数错误!";
										errCode = 2001;
									}
								}
							
					}else {
						repStr = "请添加要获取文章列表的用户id!";
						errCode = 2026;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null) {
			response.defaultSucess(listRep);
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}

	/**
	 * 根据articleId获取文章内容
	 * @param request
	 * @param articleReq
	 * @return
	 */
	@RequestMapping("/get")
	@ResponseBody
	public Object getArticle(HttpServletRequest request, @RequestBody ArticleReq articleReq) {
		String repStr = null;
		int errCode = 0;
		boolean okFlag = true;
		ArticleRep aRep = new ArticleRep();
		if (articleReq.getUserId() == null || articleReq.getToken() == null || articleReq.getArticleId()<1) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				articleReq.setUserId(Base64CC.decode(articleReq.getUserId()));
				articleReq.setToken(Base64CC.decode(articleReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(articleReq.getUserId(), articleReq.getToken());
				if (loginConfirm == 0) {
					//根据articleId获取
					Article article = articleService.getArticle(articleReq.getArticleId());
					List<Content> contents = articleService.getContents(articleReq.getArticleId());
					List<ContentRep> contentReps = new ArrayList<ContentRep>();
					if (!contents.isEmpty()) {
						for (Content content : contents) {
							ContentRep contentRep = new ContentRep();
							contentRep.setAudioUrl(Base64CC.encode(content.getAudioUrl()));
							contentRep.setAuthor(Base64CC.encode(content.getAuthor()));
							contentRep.setContent(Base64CC.encode(content.getContent()));
							contentRep.setContentId(content.getContentId());
							contentRep.setFontColor(Base64CC.encode(content.getFontColor()));
							contentRep.setFontSize(content.getFontSize());
							contentRep.setSerialNum(content.getSerialNum());
							contentRep.setVideoUrl(Base64CC.encode(content.getVideoUrl()));
							List<Img> imgs = articleService.getImgs(content.getContentId());
							List<ImgRep> imgReps = new ArrayList<ImgRep>();
							if (!imgs.isEmpty()) {
								for (Img img : imgs) {
									ImgRep imgRep = new ImgRep();
									imgRep.setImgId(img.getImgId());
									imgRep.setImgPosition(img.getImgPosition());
									imgRep.setImgSize(Base64CC.encode(img.getImgSize()));
									imgRep.setImgUrl(Base64CC.encode(img.getImgUrl()));
									imgReps.add(imgRep);
								}
								contentRep.setImgs(imgReps);
							}
							contentReps.add(contentRep);
						}
					}
					
					List<CommentRep> commentReps = new ArrayList<CommentRep>();
					List<Comment> comments = articleService.getComments(articleReq.getArticleId());
					if (!comments.isEmpty()) {
						for (Comment comment : comments) {
							CommentRep commentRep = new CommentRep();
							commentRep.setCommentId(comment.getCommentId());
							commentRep.setContent(Base64CC.encode(comment.getContent()));
							commentRep.setCreateTime(Base64CC.encode(comment.getCreateTime()));
							commentRep.setLoves(comment.getLoves());
							commentRep.setName(Base64CC.encode(comment.getName()));
							commentRep.setUserId(Base64CC.encode(comment.getUserId()));
							commentRep.setSerialNum(comment.getSerialNum());
							List<Reply> replys = articleService.getReplys(comment.getCommentId());
							List<ReplyRep> replyReps = new ArrayList<ReplyRep>();
							if (!replys.isEmpty()) {
								for (Reply reply : replys) {
									ReplyRep replyRep = new ReplyRep();
									replyRep.setContent(Base64CC.encode(reply.getContent()));
									replyRep.setCreateTime(Base64CC.encode(reply.getCreateTime()));
									replyRep.setLoves(reply.getLoves());
									replyRep.setName(Base64CC.encode(reply.getName()));
									reply.setUserId(Base64CC.encode(reply.getUserId()));
									replyRep.setReplyId(reply.getReplyId());
									replyRep.setSerialNum(reply.getSerialNum());
									replyReps.add(replyRep);
								}
								commentRep.setReps(replyReps);
							}
							commentReps.add(commentRep);
						}
					}
					aRep.setAnalysis(Base64CC.encode(article.getAnalysis()));
					aRep.setArticleClass(article.getArticleClass());
					aRep.setArticleId(article.getArticleId());
					aRep.setArticleProfile(Base64CC.encode(article.getArticleProfile()));
					aRep.setArticleStatus(article.getArticleStatus());
					aRep.setAuthor(Base64CC.encode(article.getAuthor()));
					aRep.setBook(Base64CC.encode(article.getBook()));
					aRep.setBookId(article.getBookId());
					aRep.setCountry(Base64CC.encode(article.getCountry()));
					aRep.setCreateTime(Base64CC.encode(article.getCreateTime()));
					aRep.setDynasty(Base64CC.encode(article.getDynasty()));
					aRep.setExamine(article.getExamine());
					aRep.setIsOpen(article.getIsOpen());
					aRep.setJurisdiction(Base64CC.encode(article.getJurisdiction()));
					aRep.setLoves(article.getLoves());
					aRep.setStyle(Base64CC.encode(article.getStyle()));
					aRep.setTags(Base64CC.encode(article.getTags()));
					aRep.setTitle(Base64CC.encode(article.getTitle()));
					aRep.setType(article.getType());
					aRep.setUserId(Base64CC.encode(article.getUserId()));
					aRep.setComments(commentReps);
					aRep.setContents(contentReps);
					
					if (UserMap.UserId2TokenMap.get(articleReq.getUserId()).getRole() < 3) {
						//管理员
						okFlag = true;
					}else {
						if (articleReq.getUserId().equals(article.getUserId())) {
							//自己
							if (aRep.getArticleStatus() > 2) {
								okFlag = false;
							}
						}else {
							//其他用户
							if (aRep.getArticleStatus() > 2 || aRep.getExamine() < 3 || aRep.getIsOpen() == 1) {
								okFlag = false;
							}
						}
					}
					if (!okFlag) {
						if (aRep.getArticleStatus() == 3) {
							repStr = "文章已经下架";
							errCode = 2033;
						}else if (aRep.getArticleStatus() == 4) {
							repStr = "文章已经删除";
							errCode = 2034;
						}
					}
					
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null && okFlag == true) {
			response.defaultSucess(aRep);
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}
	
	/**
	 * 改变文章的状态
	 * @param request
	 * @param statusReq
	 * @return
	 */
	@RequestMapping("/chgStatus")
	@ResponseBody
	public Object changeArticleStatus(HttpServletRequest request, @RequestBody ChgStatusReq statusReq){
		String repStr = null;
		int errCode = 0;
		if (statusReq.getUserId() == null || statusReq.getToken() == null || statusReq.getArticleId()<1 || statusReq.getArticleStatus()<1 || statusReq.getArticleStatus()>4) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				statusReq.setUserId(Base64CC.decode(statusReq.getUserId()));
				statusReq.setToken(Base64CC.decode(statusReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(statusReq.getUserId(), statusReq.getToken());
				if (loginConfirm == 0) {
					Article article = articleService.getArticle(statusReq.getArticleId());
					if (UserMap.UserId2TokenMap.get(statusReq.getUserId()).getRole() < 3) {
						/**
						 * 管理员
						 * 可以更改所有状态[1-草稿,2-发布,3-下架,4-删除]
						 */
						articleService.setArticleStatus(statusReq);
					}else if (statusReq.getUserId().equals(article.getUserId())) {
						/**
						 * 自己
						 * 可以更改状态[1-草稿,2-发布]
						 */
						if (statusReq.getArticleStatus()==1 || statusReq.getArticleStatus()==2) {
							articleService.setArticleStatus(statusReq);
						} else {
							repStr = "用户权限不够!";
							errCode = 2015;
						}
					}else {
						repStr = "用户权限不够!";
						errCode = 2015;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null) {
			try {
				response.defaultSucess(Base64CC.encode("文章状态更改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}
	
	/**
	 * 改变文章的审核状态
	 * @param request
	 * @param examineReq
	 * @return
	 */
	@RequestMapping("/chgExamine")
	@ResponseBody
	public Object changeArticleExamine(HttpServletRequest request, @RequestBody ChgExamineReq examineReq){
		String repStr = null;
		int errCode = 0;
		if (examineReq.getUserId() == null || examineReq.getToken() == null || examineReq.getArticleId()<1 || examineReq.getExamine()<1 || examineReq.getExamine()>3) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				examineReq.setUserId(Base64CC.decode(examineReq.getUserId()));
				examineReq.setToken(Base64CC.decode(examineReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(examineReq.getUserId(), examineReq.getToken());
				if (loginConfirm == 0) {
					if (UserMap.UserId2TokenMap.get(examineReq.getUserId()).getRole() < 3) {
						/**
						 * 管理员
						 * 可以更改所有状态[1--未审核，2--正在审核，3--审核通过]
						 */
						articleService.setArticleExamine(examineReq);
					}else {
						repStr = "用户权限不够!";
						errCode = 2015;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null) {
			try {
				response.defaultSucess(Base64CC.encode("文章审核进度更改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}

	/**
	 * 改变文章的推荐状态
	 * @param request
	 * @param recommendReq
	 * @return
	 */
	@RequestMapping("/chgRecommend")
	@ResponseBody
	public Object changeArticleRecommend(HttpServletRequest request, @RequestBody ChgRecommendReq recommendReq){
		String repStr = null;
		int errCode = 0;
		if (recommendReq.getUserId() == null || recommendReq.getToken() == null || recommendReq.getArticleId()<1 || recommendReq.getRecommend()<1 || recommendReq.getRecommend()>5) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				recommendReq.setUserId(Base64CC.decode(recommendReq.getUserId()));
				recommendReq.setToken(Base64CC.decode(recommendReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(recommendReq.getUserId(), recommendReq.getToken());
				if (loginConfirm == 0) {
					if (UserMap.UserId2TokenMap.get(recommendReq.getUserId()).getRole() < 3) {
						/**
						 * 管理员
						 * 可以更改所有状态[1--没有推荐;2--推荐到公众号;3--推荐到小程序;4--推荐到app;5--推荐到其他媒体]
						 */
						articleService.setArticleRecommend(recommendReq);
					}else {
						repStr = "用户权限不够!";
						errCode = 2015;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null) {
			try {
				response.defaultSucess(Base64CC.encode("文章推荐情况更改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}
	
	/**
	 * 改变文章的公开情况设置(1--仅自己可见;2--圈子内人可见;3--所有人可见)
	 * @param request
	 * @param isOpenReq
	 * @return
	 */
	@RequestMapping("/chgIsOpen")
	@ResponseBody
	public Object changeArticleIsOpen(HttpServletRequest request, @RequestBody ChgIsOpenReq isOpenReq){
		String repStr = null;
		int errCode = 0;
		if (isOpenReq.getUserId() == null || isOpenReq.getToken() == null || isOpenReq.getArticleId()<1 || isOpenReq.getIsOpen()<1 || isOpenReq.getIsOpen()>3) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				isOpenReq.setUserId(Base64CC.decode(isOpenReq.getUserId()));
				isOpenReq.setToken(Base64CC.decode(isOpenReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(isOpenReq.getUserId(), isOpenReq.getToken());
				if (loginConfirm == 0) {
					Article article = articleService.getArticle(isOpenReq.getArticleId());
					if (UserMap.UserId2TokenMap.get(isOpenReq.getUserId()).getRole() < 3) {
						/**
						 * 管理员
						 * 可以更改所有状态[1--仅自己可见;2--圈子内人可见;3--所有人可见]
						 */
						articleService.setArticleIsOpen(isOpenReq);
					}else if (article.getUserId().equals(isOpenReq.getUserId())) {
						/**
						 * 自己
						 * 可以更改所有状态[1--仅自己可见;2--圈子内人可见;3--所有人可见]
						 */
						articleService.setArticleIsOpen(isOpenReq);
					}else {
						repStr = "用户权限不够!";
						errCode = 2015;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null) {
			try {
				response.defaultSucess(Base64CC.encode("文章公开状态更改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}
	
	/**
	 * 改变文章类别(1--系统文章,2--用户文章)
	 * @param request
	 * @param isOpenReq
	 * @return
	 */
	@RequestMapping("/chgClass")
	@ResponseBody
	public Object changeArticleClass(HttpServletRequest request, @RequestBody ChgArticleClassReq classReq){
		String repStr = null;
		int errCode = 0;
		if (classReq.getUserId() == null || classReq.getToken() == null || classReq.getArticleId()<1 || classReq.getArticleClass()<1 || classReq.getArticleClass()>2) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				classReq.setUserId(Base64CC.decode(classReq.getUserId()));
				classReq.setToken(Base64CC.decode(classReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(classReq.getUserId(), classReq.getToken());
				if (loginConfirm == 0) {
					if (UserMap.UserId2TokenMap.get(classReq.getUserId()).getRole() < 3) {
						/**
						 * 管理员
						 * 可以更改文章类别[1--系统文章,2--用户文章]
						 */
						articleService.setArticleClass(classReq);
					}else{
						repStr = "用户权限不够!";
						errCode = 2015;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		/**
		 * 判定返回,并对返回数据进行Base64编码
		 */
		String encodeRepStr = null;
		if (repStr != null) {
			try {
				encodeRepStr = Base64CC.encode(repStr);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		ResponseUtil response = new ResponseUtil();
		if (repStr == null) {
			try {
				response.defaultSucess(Base64CC.encode("文章类别更改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr" + repStr);
			response.defaultFailed(errCode, encodeRepStr);
			return response;
		}
	}
	
	@RequestMapping("/chgLoves")
	@ResponseBody
	public Object changeArticleLoves(HttpServletRequest request){
		
		return null;
	}
	
	/**
	 * 添加评论
	 * 
	 * @param request
	 * @param token
	 * @param userid
	 * @param comment
	 * @return
	 */
	@RequestMapping("/addComment")
	@ResponseBody
	public Object addComment(HttpServletRequest request, @RequestBody AddCommentReq addReq) {
		String repStr = null;
		int errCode = 0;
		if (addReq.getUserId() == null || addReq.getToken() == null || addReq.getComment()==null) {
			repStr = "参数上传错误!";
			errCode = 2001;
		} else {
			try {
				/**
				 * base64解码
				 */
				addReq.setUserId(Base64CC.decode(addReq.getUserId()));
				addReq.setToken(Base64CC.decode(addReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(addReq.getUserId(), addReq.getToken());
				if (loginConfirm == 0) {
					
					
					
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}	
		return "this is a test of updatearticle";
	}

	/**
	 * 回复评论
	 * 
	 * @param request
	 * @param token
	 * @param userid
	 * @param reply
	 * @return
	 */
	@RequestMapping("/addreply")
	@ResponseBody
	public Object addrelpy(HttpServletRequest request, @RequestParam(value = "token") String token,
			@RequestParam(value = "userid") String userid, @RequestParam(value = "reply") String reply) {

		return "this is a test of updatearticle";
	}

	/**
	 * 更新content表里的imgId
	 * 
	 * @param contentId
	 * @return
	 */
	public int updateImgIds(int contentId) {
		try {
			List<Img> imgs = articleService.getImgs(contentId);
			// 把插图id插入到content的插图id数组
			String imgIdStr = "[";
			if (imgs.size() > 0) {
				for (Img img2 : imgs) {
					imgIdStr = imgIdStr + String.valueOf(img2.getImgId()) + ",";
				}
				imgIdStr = imgIdStr.substring(0, imgIdStr.length() - 1);
				imgIdStr = imgIdStr + "]";
				Content content2 = new Content();
				content2.setContentId(contentId);
				content2.setImgIds(imgIdStr);
				articleService.updateImgIds(content2);
			}else {
				Content content2 = new Content();
				content2.setContentId(contentId);
				content2.setImgIds("[]");
				articleService.updateImgIds(content2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}

		return 0;
	}
	
	/**
	 * 更新article表里的contentIds
	 * 
	 * @param articleId
	 * @return
	 */
	public int updateContentIds(int articleId) {
		try {
			List<Content> contents = articleService.getContents(articleId);
			// 把文段id插入到article的文段id数组
			String contentIdStr = "[";
			if (contents.size() > 0) {
				for (Content content : contents) {
					contentIdStr = contentIdStr + String.valueOf(content.getContentId()) + ",";
				}
				contentIdStr = contentIdStr.substring(0, contentIdStr.length() - 1);
				contentIdStr = contentIdStr + "]";
				Article article = new Article();
				article.setContentIds(contentIdStr);
				article.setArticleId(articleId);
				articleService.updateContentIds(article);
			}else {
				Article article = new Article();
				article.setContentIds("[]");
				article.setArticleId(articleId);
				articleService.updateContentIds(article);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}

		return 0;
	}
	
	
	public int updateContentImgs(ArticleContentInfo newContent,int oldContentId){
		/**
		 * 1.根据oldContentId从数据库获取文段内容
		 */
		try {
			Content oldContent = articleService.getContentByContentId(oldContentId);
			List<ContentImgInfo> newImgs = newContent.getImgs();
			
			
			if (oldContent.getImgIds() != null) {
				//旧文章存在插图
				JSONArray oldImgIdArr = new JSONArray(oldContent.getImgIds());
				System.out.println("newImgsSize = " + newImgs.size() + " ; oldImgIdArrSize = " + oldImgIdArr.length());
				System.out.println("oldContentImgIds = " + oldContent.getImgIds());
				if (oldImgIdArr.length() >= newImgs.size()) {
					/**
					 * 文段旧的插图数大于新的插图数
					 * 新的覆盖旧的,多余的旧的删除
					 */
					
					for (int i = 0; i < newImgs.size(); i++) {
						Img oldImg = articleService.getImgByImgId(oldImgIdArr.getInt(i));
						ContentImgInfo newImg = newImgs.get(i);
						System.out.println("oldImgId = " + oldImg.getImgId());
						
						if (newImg.getImgUrl() != null) {
							oldImg.setImgUrl(Base64CC.decode(newImg.getImgUrl()));
						}else {
							oldImg.setImgUrl("");
						}
						if (newImg.getImgPosition() > 0) {
							oldImg.setImgPosition(newImg.getImgPosition());
						}
						if (newImg.getImgSize() != null) {
							oldImg.setImgSize(Base64CC.decode(newImg.getImgSize()));
						}
						//更新到数据库
						articleService.updateImgByImgId(oldImg);
					}
					for (int i = newImgs.size(); i < oldImgIdArr.length(); i++) {
						articleService.deleteImgByImgId(oldImgIdArr.getInt(i));
					}
				} else {
					/**
					 * 文段旧的插图数小于新的插图数
					 * 新的覆盖旧的,插入新的
					 */
					for (int i = 0; i < oldImgIdArr.length(); i++) {
						Img oldImg = articleService.getImgByImgId(oldImgIdArr.getInt(i));
						ContentImgInfo newImg = newImgs.get(i);
						if (newImg.getImgUrl() != null) {
							oldImg.setImgUrl(Base64CC.decode(newImg.getImgUrl()));
						}else {
							oldImg.setImgUrl("");
						}
						if (newImg.getImgPosition() > 0) {
							oldImg.setImgPosition(newImg.getImgPosition());
						}
						if (newImg.getImgSize() != null) {
							oldImg.setImgSize(newImg.getImgSize());
						}
						//更新到数据库
						articleService.updateImgByImgId(oldImg);
					}
					
					for (int i = oldImgIdArr.length(); i < newImgs.size(); i++) {
						// 插入插图
						Img img = new Img();
						img.setArticleId(oldContent.getArticleId());
						img.setContentId(oldContent.getContentId());
						if (newImgs.get(i).getImgPosition() == 0) {
							img.setImgPosition(3);
						} else {
							img.setImgPosition(newImgs.get(i).getImgPosition());
						}
						img.setImgUrl(Base64CC.decode(newImgs.get(i).getImgUrl()));
						img.setImgSize(Base64CC.decode(newImgs.get(i).getImgSize()));
						img.setUserId(oldContent.getUserId());

						articleService.insertOneImg(img);
					}
				}
				updateImgIds(oldContent.getContentId());
			}else {
				/**
				 * 旧文章不存在插图
				 * 直接插入图片
				 */
				for (int i = 0; i < newImgs.size(); i++) {
					// 插入插图
					Img img = new Img();
					img.setArticleId(oldContent.getArticleId());
					img.setContentId(oldContent.getContentId());
					if (newImgs.get(i).getImgPosition() == 0) {
						img.setImgPosition(3);
					} else {
						img.setImgPosition(newImgs.get(i).getImgPosition());
					}
					img.setImgUrl(Base64CC.decode(newImgs.get(i).getImgUrl()));
					img.setUserId(oldContent.getUserId());

					articleService.insertOneImg(img);
				}
				updateImgIds(oldContent.getContentId());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
